import java.io.*;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.interpolators.*;
import java.util.Enumeration;


public class Podio extends IndexedQuadArray {

    public Podio (int vertexCount, int vertexFormat, int indexCount) {
                    super (vertexCount, vertexFormat, indexCount);

            calcularPmed();
            calcularPcam();
            calcularVcam();

            setCoorParteSup();
            setCoorParteMed();
            setCoorParteInf();
            setCoorCamara();

            int i;
            i=setCoorInParteSup(0);
            i=setCoorInParteMed(i);
            i=setCoorInParteInf(i);
            setCoorInCamara(i);

    this.setColor( 0, new Color3f(0.60f, 0.60f, 0.60f));
    this.setColor( 1, new Color3f(0.54f, 0.54f, 0.54f));
    this.setColor( 2, new Color3f(0.48f, 0.48f, 0.48f));
    this.setColor( 3, new Color3f(0.42f, 0.42f, 0.42f));
    this.setColor( 4, new Color3f(0.36f, 0.36f, 0.36f));

    this.setColor( 5, new Color3f(0.58f, 0.58f, 0.58f));
    this.setColor( 6, new Color3f(0.52f, 0.52f, 0.52f));
    this.setColor( 7, new Color3f(0.46f, 0.46f, 0.46f));
    this.setColor( 8, new Color3f(0.40f, 0.40f, 0.40f));
    this.setColor( 9, new Color3f(0.34f, 0.34f, 0.34f));

    this.setColor(10, new Color3f(0.56f, 0.56f, 0.56f));
    this.setColor(11, new Color3f(0.50f, 0.50f, 0.50f));
    this.setColor(12, new Color3f(0.44f, 0.44f, 0.44f));
    this.setColor(13, new Color3f(0.38f, 0.38f, 0.38f));

    this.setColor(14, new Color3f(0.30f, 0.30f, 0.30f));
    this.setColor(15, new Color3f(0.25f, 0.25f, 0.25f));
    this.setColor(16, new Color3f(0.20f, 0.20f, 0.20f));
    this.setColor(17, new Color3f(0.80f, 0.80f, 0.80f));
    }

    public void calcularPmed(){
            p_med.x = p_sup.x+( largo_bra_sup*Math.sin( Math.toRadians(inc_sup) )*Math.cos( Math.toRadians(rot_sup) ) );
            p_med.y = p_sup.y-( largo_bra_sup*Math.cos( Math.toRadians(inc_sup) ) );
            p_med.z = p_sup.z+( largo_bra_sup*Math.sin( Math.toRadians(inc_sup) )*Math.sin( Math.toRadians(rot_sup) ) );
//		System.out.println(p_med);
    }

    public void calcularPcam(){
            p_cam.x = p_med.x+( (largo_bra_inf)*Math.sin( Math.toRadians(inc_sup-inc_med) )*Math.cos( Math.toRadians(rot_sup) ) );
            p_cam.y = p_med.y-( (largo_bra_inf)*Math.cos( Math.toRadians(inc_sup-inc_med) ) );
            p_cam.z = p_med.z+( (largo_bra_inf)*Math.sin( Math.toRadians(inc_sup-inc_med) )*Math.sin( Math.toRadians(rot_sup) ) );
//		System.out.println(p_cam);
    }

    public void calcularVcam(){
//		v_cam = calcularVector( 180+rot_sup+rot_cam,inc_med-inc_sup );
            v_cam = new Vector3d( p_cam.x-p_med.x,p_cam.y-p_med.y,p_cam.z-p_med.z );
            v_cam.normalize();
//		double rot = 180+rot_sup-rot_cam;
//		double inc = inc_med-inc_sup;
//		v_cam.x = Math.cos( Math.toRadians(rot) )*Math.cos( Math.toRadians(inc*Math.cos( Math.toRadians(rot_cam) )) );
//		v_cam.y = Math.sin( Math.toRadians(inc*Math.cos( Math.toRadians(rot_cam) )) );
//		v_cam.z = Math.sin( Math.toRadians(rot) )*Math.cos( Math.toRadians(inc*Math.cos( Math.toRadians(rot_cam) )) );
//		System.out.println(v_cam);
//		System.out.println(rot);
//		System.out.println(inc);
    }

    public Vector3d calcularVector( double rot, double inc ){
            Vector3d v = new Vector3d();
            v.x = Math.cos( Math.toRadians(rot) )*Math.cos( Math.toRadians(inc) );
            v.y = Math.sin( Math.toRadians(inc) );
            v.z = Math.sin( Math.toRadians(rot) )*Math.cos( Math.toRadians(inc) );
            return v;
    }

    public Point3d pdv(Point3d p, double d, Vector3d v){
            p = new Point3d( p.x+d*v.x,p.y+d*v.y,p.z+d*v.z );
            return p;
    }

    public Point3d pda(Point3d p, double d, double a){
            p = new Point3d( p.x+d*Math.sin(-Math.toRadians(a)),p.y,p.z+d*Math.cos(-Math.toRadians(a)) );
            return p;
    }

    public void setCoorParteSup(){
    	double ancho_bs = 1.2;
    	double d = ancho_bs/2;
		double a = rot_sup;
		Vector3d v = calcularVector( rot_sup, 0 );

		Point3d pc = new Point3d(p_sup.x,p_sup.y+2.5,p_sup.z);
        this.setCoordinate( 0, pdv( pda(pc,d,a),d,v ) );
        this.setCoordinate( 1, pdv( pda(pc,-d,a),d,v ) );
        this.setCoordinate( 2, pdv( pda(pc,-d,a),-d,v ) );
        this.setCoordinate( 3, pdv( pda(pc,d,a),-d,v ) );
		pc = new Point3d(p_sup.x,p_sup.y-.4,p_sup.z);
        this.setCoordinate( 4, pdv( pda(pc,d,a),d,v ) );
        this.setCoordinate( 5, pdv( pda(pc,-d,a),d,v ) );
        this.setCoordinate( 6, pdv( pda(pc,-d,a),-d,v ) );
        this.setCoordinate( 7, pdv( pda(pc,d,a),-d,v ) );
        
        this.setCoordinate( 8, pdv( pda(pc,d-.05,a),d,v ) );
        this.setCoordinate( 9, pdv( pda(pc,-d+.05,a),d,v ) );
        this.setCoordinate(10, pdv( pda(pc,-d+.05,a),-d,v ) );
        this.setCoordinate(11, pdv( pda(pc,d-.05,a),-d,v ) );
		pc = new Point3d(p_sup.x,p_sup.y+2.3,p_sup.z);
        this.setCoordinate(12, pdv( pda(pc,d-.05,a),d,v ) );
        this.setCoordinate(13, pdv( pda(pc,-d+.05,a),d,v ) );
        this.setCoordinate(14, pdv( pda(pc,-d+.05,a),-d,v ) );
        this.setCoordinate(15, pdv( pda(pc,d-.05,a),-d,v ) );
        
        this.setCoordinate(16, pdv( pda(p_sup,d+.05,a),.1,v ) );
        this.setCoordinate(20, pdv( pda(p_sup,-d-.05,a),.1,v ) );
        this.setCoordinate(22, pdv( pda(p_sup,-d-.05,a),-.1,v ) );
        this.setCoordinate(18, pdv( pda(p_sup,d+.05,a),-.1,v ) );
		pc = new Point3d(p_sup.x,p_sup.y+.1,p_sup.z);
        this.setCoordinate(17, pda(pc,d+.05,a) );
        this.setCoordinate(21, pda(pc,-d-.05,a) );
		pc = new Point3d(p_sup.x,p_sup.y-.1,p_sup.z);
        this.setCoordinate(23, pda(pc,-d-.05,a) );
        this.setCoordinate(19, pda(pc,d+.05,a) );

		n1 = 24;
    }
    
    public void setCoorParteMed(){
    	double ancho_bm = .75;
    	double d = ancho_bm/2;
		double dc = 1.8;
		double a = rot_sup;
		Vector3d v = calcularVector( rot_sup, inc_sup );
		Vector3d c = new Vector3d( p_sup.x-p_med.x,p_sup.y-p_med.y,p_sup.z-p_med.z );
		c.normalize();

		Point3d pc = pdv( p_sup,dc,c );
        this.setCoordinate(   n1, pdv( pda(pc,d,a),d,v ) );
        this.setCoordinate( n1+1, pdv( pda(pc,-d,a),d,v ) );
        this.setCoordinate( n1+2, pdv( pda(pc,-d,a),-d,v ) );
        this.setCoordinate( n1+3, pdv( pda(pc,d,a),-d,v ) );
		pc = pdv( p_med,-.375,c );
        this.setCoordinate( n1+4, pdv( pda(pc,d,a),d,v ) );
        this.setCoordinate( n1+5, pdv( pda(pc,-d,a),d,v ) );
        this.setCoordinate( n1+6, pdv( pda(pc,-d,a),-d,v ) );
        this.setCoordinate( n1+7, pdv( pda(pc,d,a),-d,v ) );
        
        this.setCoordinate( n1+8, pdv( pda(pc,d-.05,a),d,v ) );
        this.setCoordinate( n1+9, pdv( pda(pc,-d+.05,a),d,v ) );
        this.setCoordinate(n1+10, pdv( pda(pc,-d+.05,a),-d,v ) );
        this.setCoordinate(n1+11, pdv( pda(pc,d-.05,a),-d,v ) );
		pc = pdv( p_sup,.4,c );
        this.setCoordinate(n1+12, pdv( pda(pc,d-.05,a),d,v ) );
        this.setCoordinate(n1+13, pdv( pda(pc,-d+.05,a),d,v ) );
        this.setCoordinate(n1+14, pdv( pda(pc,-d+.05,a),-d,v ) );
        this.setCoordinate(n1+15, pdv( pda(pc,d-.05,a),-d,v ) );

        this.setCoordinate(n1+16, pdv( pda(p_med,d+.05,a),.1,v ) );
        this.setCoordinate(n1+20, pdv( pda(p_med,-d-.05,a),.1,v ) );
        this.setCoordinate(n1+22, pdv( pda(p_med,-d-.05,a),-.1,v ) );
        this.setCoordinate(n1+18, pdv( pda(p_med,d+.05,a),-.1,v ) );
		pc = pdv( p_med,.1,c );
        this.setCoordinate(n1+17, pda(pc,d+.05,a) );
        this.setCoordinate(n1+21, pda(pc,-d-.05,a) );
		pc = pdv( p_med,-.1,c );
        this.setCoordinate(n1+23, pda(pc,-d-.05,a) );
        this.setCoordinate(n1+19, pda(pc,d+.05,a) );

		n2 = n1 + 24;
	}
    
    public void setCoorParteInf(){
    	double ancho_bi = .4;
    	double d = ancho_bi/2;
		double a = rot_sup;
		Vector3d v = calcularVector( rot_sup, inc_sup-inc_med );
		Vector3d c = getVcam();//new Vector3d( p_med.x-p_cam.x,p_med.y-p_cam.y,p_med.z-p_cam.z );
		//c.normalize();

		Point3d pc = pdv( p_med,-.2,c );
        this.setCoordinate(  n2, pdv( pda(pc,d,a),d,v ) );
        this.setCoordinate(n2+1, pdv( pda(pc,-d,a),d,v ) );
        this.setCoordinate(n2+2, pdv( pda(pc,-d,a),-d,v ) );
        this.setCoordinate(n2+3, pdv( pda(pc,d,a),-d,v ) );
		pc = p_cam;
        this.setCoordinate(n2+4, pdv( pda(pc,d,a),d,v ) );
        this.setCoordinate(n2+5, pdv( pda(pc,-d,a),d,v ) );
        this.setCoordinate(n2+6, pdv( pda(pc,-d,a),-d,v ) );
        this.setCoordinate(n2+7, pdv( pda(pc,d,a),-d,v ) );

		n3 = n2+8;
    }
    
    public void setCoorCamara(){
        double l = largo_camara;
        double d = .25/Math.cos( Math.toRadians(45) );

        double a = rot_sup;
		Vector3d v = calcularVector( rot_sup, inc_sup-inc_med );
		Vector3d c = getVcam();//new Vector3d( p_med.x-p_cam.x,p_med.y-p_cam.y,p_med.z-p_cam.z );
		//c.normalize();
		double cr = d*Math.cos( Math.toRadians(rot_cam+45) );
		double sr = d*Math.sin( Math.toRadians(rot_cam+45) );
        
		Point3d pc = p_cam;
        this.setCoordinate(  n3, pdv( pda(pc,cr,a),-sr,v ) );
        this.setCoordinate(n3+1, pdv( pda(pc,sr,a),cr,v ) );
        this.setCoordinate(n3+2, pdv( pda(pc,-cr,a),sr,v ) );
        this.setCoordinate(n3+3, pdv( pda(pc,-sr,a),-cr,v ) );
		pc = pdv( p_cam,l,c );
        this.setCoordinate(n3+4, pdv( pda(pc,cr,a),-sr,v ) );
        this.setCoordinate(n3+5, pdv( pda(pc,sr,a),cr,v ) );
        this.setCoordinate(n3+6, pdv( pda(pc,-cr,a),sr,v ) );
        this.setCoordinate(n3+7, pdv( pda(pc,-sr,a),-cr,v ) );

//        this.setCoordinate(n3+8, new Point3d(0,0,0) );
//        this.setCoordinate(n3+9, pdv( new Point3d(0,0,0),1,calcularVector(180+rot_sup,90-inc_sup) ) );
    }
    
    public Vector3d getUcam() { 
        //double a = rot_sup;
		double cr = Math.cos( Math.toRadians(rot_cam) );
		double sr = Math.sin( Math.toRadians(rot_cam) );
		Vector3d v = calcularVector( rot_sup, inc_sup-inc_med );
		v.negate();
		Point3d p = pdv( pda(p_cam,-sr,rot_sup),cr,v );
		
		u_cam.x = p.x-p_cam.x;
		u_cam.y = p.y-p_cam.y;
		u_cam.z = p.z-p_cam.z;
		u_cam.normalize();
    	return u_cam; 
    }

    public int setCoorInParteSup(int i){
    	int n=0;
/*        this.setCoordinateIndex(   i, 0);
        this.setCoordinateIndex( i+1, 1);
        this.setCoordinateIndex( i+2, 1);
        this.setCoordinateIndex( i+3, 2);
        this.setCoordinateIndex( i+4, 2);
        this.setCoordinateIndex( i+5, 3);
        this.setCoordinateIndex( i+6, 3);
        this.setCoordinateIndex( i+7, 0);
        
        this.setCoordinateIndex( i+8, 0);
        this.setCoordinateIndex( i+9, 4);
        this.setCoordinateIndex(i+10, 1);
        this.setCoordinateIndex(i+11, 5);
        this.setCoordinateIndex(i+12, 2);
        this.setCoordinateIndex(i+13, 6);
        this.setCoordinateIndex(i+14, 3);
        this.setCoordinateIndex(i+15, 7);
        
        this.setCoordinateIndex(i+16, 5);
        this.setCoordinateIndex(i+17, 6);
        this.setCoordinateIndex(i+18, 7);
        this.setCoordinateIndex(i+19, 4);
		
        this.setCoordinateIndex(i+20, 4);
        this.setCoordinateIndex(i+21, 8);
        this.setCoordinateIndex(i+22, 5);
        this.setCoordinateIndex(i+23, 9);
        this.setCoordinateIndex(i+24, 6);
        this.setCoordinateIndex(i+25,10);
        this.setCoordinateIndex(i+26, 7);
        this.setCoordinateIndex(i+27,11);

        this.setCoordinateIndex(i+28, 8);
        this.setCoordinateIndex(i+29,12);
        this.setCoordinateIndex(i+30, 9);
        this.setCoordinateIndex(i+31,13);
        this.setCoordinateIndex(i+32,10);
        this.setCoordinateIndex(i+33,14);
        this.setCoordinateIndex(i+34,11);
        this.setCoordinateIndex(i+35,15);
        
        this.setCoordinateIndex(i+36,12);
        this.setCoordinateIndex(i+37,13);
        this.setCoordinateIndex(i+38,13);
        this.setCoordinateIndex(i+39,14);
        this.setCoordinateIndex(i+40,14);
        this.setCoordinateIndex(i+41,15);
        this.setCoordinateIndex(i+42,15);
        this.setCoordinateIndex(i+43,12);
        
        this.setCoordinateIndex(i+44,16);
        this.setCoordinateIndex(i+45,17);
*/
        this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+2);
        this.setCoordinateIndex( i+3, n+3);
        
        this.setCoordinateIndex( i+4, n+5);
        this.setCoordinateIndex( i+5, n+6);
        this.setCoordinateIndex( i+6, n+2);
        this.setCoordinateIndex( i+7, n+1);
        
        this.setCoordinateIndex( i+8, n+7);
        this.setCoordinateIndex( i+9, n+4);
        this.setCoordinateIndex(i+10, n+0);
        this.setCoordinateIndex(i+11, n+3);
        
        this.setCoordinateIndex(i+12, n+12);
        this.setCoordinateIndex(i+13, n+13);
        this.setCoordinateIndex(i+14, n+1);
        this.setCoordinateIndex(i+15, n+0);
        
        this.setCoordinateIndex(i+16, n+0);
        this.setCoordinateIndex(i+17, n+4);
        this.setCoordinateIndex(i+18, n+8);
        this.setCoordinateIndex(i+19, n+12);
        
        this.setCoordinateIndex(i+20, n+13);
        this.setCoordinateIndex(i+21, n+9);
        this.setCoordinateIndex(i+22, n+5);
        this.setCoordinateIndex(i+23, n+1);

        this.setCoordinateIndex(i+24, n+14);
        this.setCoordinateIndex(i+25, n+15);
        this.setCoordinateIndex(i+26, n+3);
        this.setCoordinateIndex(i+27, n+2);
        
        this.setCoordinateIndex(i+28, n+2);
        this.setCoordinateIndex(i+29, n+6);
        this.setCoordinateIndex(i+30, n+10);
        this.setCoordinateIndex(i+31, n+14);
        
        this.setCoordinateIndex(i+32, n+15);
        this.setCoordinateIndex(i+33, n+11);
        this.setCoordinateIndex(i+34, n+7);
        this.setCoordinateIndex(i+35, n+3);

        this.setCoordinateIndex(i+36, n+13);
        this.setCoordinateIndex(i+37, n+14);
        this.setCoordinateIndex(i+38, n+10);
        this.setCoordinateIndex(i+39, n+9);
        
        this.setCoordinateIndex(i+40, n+15);
        this.setCoordinateIndex(i+41, n+12);
        this.setCoordinateIndex(i+42, n+8);
        this.setCoordinateIndex(i+43, n+11);

        this.setCoordinateIndex(i+44, n+15);
        this.setCoordinateIndex(i+45, n+14);
        this.setCoordinateIndex(i+46, n+13);
        this.setCoordinateIndex(i+47, n+12);

        this.setCoordinateIndex(i+48, n+6);
        this.setCoordinateIndex(i+49, n+5);
        this.setCoordinateIndex(i+50, n+9);
        this.setCoordinateIndex(i+51, n+10);
        
        this.setCoordinateIndex(i+52, n+4);
        this.setCoordinateIndex(i+53, n+7);
        this.setCoordinateIndex(i+54, n+11);
        this.setCoordinateIndex(i+55, n+8);

        this.setCoordinateIndex(i+56, n+16);
        this.setCoordinateIndex(i+57, n+17);
        this.setCoordinateIndex(i+58, n+18);
        this.setCoordinateIndex(i+59, n+19);

        this.setCoordinateIndex(i+60, n+23);
        this.setCoordinateIndex(i+61, n+22);
        this.setCoordinateIndex(i+62, n+21);
        this.setCoordinateIndex(i+63, n+20);
        
        this.setCoordinateIndex(i+64, n+20);
        this.setCoordinateIndex(i+65, n+21);
        this.setCoordinateIndex(i+66, n+17);
        this.setCoordinateIndex(i+67, n+16);

        this.setCoordinateIndex(i+68, n+22);
        this.setCoordinateIndex(i+69, n+23);
        this.setCoordinateIndex(i+70, n+19);
        this.setCoordinateIndex(i+71, n+18);
        
        this.setCoordinateIndex(i+72, n+21);
        this.setCoordinateIndex(i+73, n+22);
        this.setCoordinateIndex(i+74, n+18);
        this.setCoordinateIndex(i+75, n+17);

        this.setCoordinateIndex(i+76, n+23);
        this.setCoordinateIndex(i+77, n+20);
        this.setCoordinateIndex(i+78, n+16);
        this.setCoordinateIndex(i+79, n+19);
        
		for (int k=i;k<i+4;k++){
	        this.setColorIndex(0+k, 0);
	        this.setColorIndex(4+k, 1);
	        this.setColorIndex(8+k, 1);
	        this.setColorIndex(12+k, 2);
	        this.setColorIndex(16+k, 2);
	        this.setColorIndex(20+k, 2);
	        this.setColorIndex(24+k, 2);
	        this.setColorIndex(28+k, 2);
	        this.setColorIndex(32+k, 2);
	        this.setColorIndex(36+k, 3);
	        this.setColorIndex(40+k, 3);
	        this.setColorIndex(44+k, 4);
	        this.setColorIndex(48+k, 4);
	        this.setColorIndex(52+k, 4);
	        this.setColorIndex(56+k,16);
	        this.setColorIndex(60+k,16);
	        this.setColorIndex(64+k,15);
	        this.setColorIndex(68+k,15);
	        this.setColorIndex(72+k,14);
	        this.setColorIndex(76+k,14);
		}

		return i+80;
    }

    public int setCoorInParteMed(int i){
		int n = n1;

/*      this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+1);
        this.setCoordinateIndex( i+3, n+2);
        this.setCoordinateIndex( i+4, n+2);
        this.setCoordinateIndex( i+5, n+3);
        this.setCoordinateIndex( i+6, n+3);
        this.setCoordinateIndex( i+7, n+0);
        
        this.setCoordinateIndex( i+8, n+0);
        this.setCoordinateIndex( i+9, n+4);
        this.setCoordinateIndex(i+10, n+1);
        this.setCoordinateIndex(i+11, n+5);
        this.setCoordinateIndex(i+12, n+2);
        this.setCoordinateIndex(i+13, n+6);
        this.setCoordinateIndex(i+14, n+3);
        this.setCoordinateIndex(i+15, n+7);
        
        this.setCoordinateIndex(i+16, n+5);
        this.setCoordinateIndex(i+17, n+6);
        this.setCoordinateIndex(i+18, n+7);
        this.setCoordinateIndex(i+19, n+4);
		
        this.setCoordinateIndex(i+20, n+4);
        this.setCoordinateIndex(i+21, n+8);
        this.setCoordinateIndex(i+22, n+5);
        this.setCoordinateIndex(i+23, n+9);
        this.setCoordinateIndex(i+24, n+6);
        this.setCoordinateIndex(i+25,n+10);
        this.setCoordinateIndex(i+26, n+7);
        this.setCoordinateIndex(i+27,n+11);

        this.setCoordinateIndex(i+28, n+8);
        this.setCoordinateIndex(i+29,n+12);
        this.setCoordinateIndex(i+30, n+9);
        this.setCoordinateIndex(i+31,n+13);
        this.setCoordinateIndex(i+32,n+10);
        this.setCoordinateIndex(i+33,n+14);
        this.setCoordinateIndex(i+34,n+11);
        this.setCoordinateIndex(i+35,n+15);
        
        this.setCoordinateIndex(i+36,n+12);
        this.setCoordinateIndex(i+37,n+13);
        this.setCoordinateIndex(i+38,n+13);
        this.setCoordinateIndex(i+39,n+14);
        this.setCoordinateIndex(i+40,n+14);
        this.setCoordinateIndex(i+41,n+15);
        this.setCoordinateIndex(i+42,n+15);
        this.setCoordinateIndex(i+43,n+12);
        
        this.setCoordinateIndex(i+44,n+16);
        this.setCoordinateIndex(i+45,n+17);
*/
        this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+2);
        this.setCoordinateIndex( i+3, n+3);
        
        this.setCoordinateIndex( i+4, n+5);
        this.setCoordinateIndex( i+5, n+6);
        this.setCoordinateIndex( i+6, n+2);
        this.setCoordinateIndex( i+7, n+1);
        
        this.setCoordinateIndex( i+8, n+7);
        this.setCoordinateIndex( i+9, n+4);
        this.setCoordinateIndex(i+10, n+0);
        this.setCoordinateIndex(i+11, n+3);
        
        this.setCoordinateIndex(i+12, n+12);
        this.setCoordinateIndex(i+13, n+13);
        this.setCoordinateIndex(i+14, n+1);
        this.setCoordinateIndex(i+15, n+0);
        
        this.setCoordinateIndex(i+16, n+0);
        this.setCoordinateIndex(i+17, n+4);
        this.setCoordinateIndex(i+18, n+8);
        this.setCoordinateIndex(i+19, n+12);
        
        this.setCoordinateIndex(i+20, n+13);
        this.setCoordinateIndex(i+21, n+9);
        this.setCoordinateIndex(i+22, n+5);
        this.setCoordinateIndex(i+23, n+1);

        this.setCoordinateIndex(i+24, n+14);
        this.setCoordinateIndex(i+25, n+15);
        this.setCoordinateIndex(i+26, n+3);
        this.setCoordinateIndex(i+27, n+2);
        
        this.setCoordinateIndex(i+28, n+2);
        this.setCoordinateIndex(i+29, n+6);
        this.setCoordinateIndex(i+30, n+10);
        this.setCoordinateIndex(i+31, n+14);
        
        this.setCoordinateIndex(i+32, n+15);
        this.setCoordinateIndex(i+33, n+11);
        this.setCoordinateIndex(i+34, n+7);
        this.setCoordinateIndex(i+35, n+3);

        this.setCoordinateIndex(i+36, n+13);
        this.setCoordinateIndex(i+37, n+14);
        this.setCoordinateIndex(i+38, n+10);
        this.setCoordinateIndex(i+39, n+9);
        
        this.setCoordinateIndex(i+40, n+15);
        this.setCoordinateIndex(i+41, n+12);
        this.setCoordinateIndex(i+42, n+8);
        this.setCoordinateIndex(i+43, n+11);

        this.setCoordinateIndex(i+44, n+15);
        this.setCoordinateIndex(i+45, n+14);
        this.setCoordinateIndex(i+46, n+13);
        this.setCoordinateIndex(i+47, n+12);

        this.setCoordinateIndex(i+48, n+6);
        this.setCoordinateIndex(i+49, n+5);
        this.setCoordinateIndex(i+50, n+9);
        this.setCoordinateIndex(i+51, n+10);
        
        this.setCoordinateIndex(i+52, n+4);
        this.setCoordinateIndex(i+53, n+7);
        this.setCoordinateIndex(i+54, n+11);
        this.setCoordinateIndex(i+55, n+8);

        this.setCoordinateIndex(i+56, n+16);
        this.setCoordinateIndex(i+57, n+17);
        this.setCoordinateIndex(i+58, n+18);
        this.setCoordinateIndex(i+59, n+19);

        this.setCoordinateIndex(i+60, n+23);
        this.setCoordinateIndex(i+61, n+22);
        this.setCoordinateIndex(i+62, n+21);
        this.setCoordinateIndex(i+63, n+20);
        
        this.setCoordinateIndex(i+64, n+20);
        this.setCoordinateIndex(i+65, n+21);
        this.setCoordinateIndex(i+66, n+17);
        this.setCoordinateIndex(i+67, n+16);

        this.setCoordinateIndex(i+68, n+22);
        this.setCoordinateIndex(i+69, n+23);
        this.setCoordinateIndex(i+70, n+19);
        this.setCoordinateIndex(i+71, n+18);
        
        this.setCoordinateIndex(i+72, n+21);
        this.setCoordinateIndex(i+73, n+22);
        this.setCoordinateIndex(i+74, n+18);
        this.setCoordinateIndex(i+75, n+17);

        this.setCoordinateIndex(i+76, n+23);
        this.setCoordinateIndex(i+77, n+20);
        this.setCoordinateIndex(i+78, n+16);
        this.setCoordinateIndex(i+79, n+19);
        
		for (int k=i;k<i+4;k++){
	        this.setColorIndex(0+k, 5);
	        this.setColorIndex(4+k, 6);
	        this.setColorIndex(8+k, 6);
	        this.setColorIndex(12+k, 7);
	        this.setColorIndex(16+k, 7);
	        this.setColorIndex(20+k, 7);
	        this.setColorIndex(24+k, 7);
	        this.setColorIndex(28+k, 7);
	        this.setColorIndex(32+k, 7);
	        this.setColorIndex(36+k, 8);
	        this.setColorIndex(40+k, 8);
	        this.setColorIndex(44+k, 9);
	        this.setColorIndex(48+k, 9);
	        this.setColorIndex(52+k, 9);
	        this.setColorIndex(56+k,16);
	        this.setColorIndex(60+k,16);
	        this.setColorIndex(64+k,15);
	        this.setColorIndex(68+k,15);
	        this.setColorIndex(72+k,14);
	        this.setColorIndex(76+k,14);
		}

		return i+80;
    }

    public int setCoorInParteInf(int i){
		int n = n2;
/*
        this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+1);
        this.setCoordinateIndex( i+3, n+2);
        this.setCoordinateIndex( i+4, n+2);
        this.setCoordinateIndex( i+5, n+3);
        this.setCoordinateIndex( i+6, n+3);
        this.setCoordinateIndex( i+7, n+0);
        
        this.setCoordinateIndex( i+8, n+0);
        this.setCoordinateIndex( i+9, n+4);
        this.setCoordinateIndex(i+10, n+1);
        this.setCoordinateIndex(i+11, n+5);
        this.setCoordinateIndex(i+12, n+2);
        this.setCoordinateIndex(i+13, n+6);
        this.setCoordinateIndex(i+14, n+3);
        this.setCoordinateIndex(i+15, n+7);
        
        this.setCoordinateIndex(i+16, n+4);
        this.setCoordinateIndex(i+17, n+5);
        this.setCoordinateIndex(i+18, n+5);
        this.setCoordinateIndex(i+19, n+6);
        this.setCoordinateIndex(i+20, n+6);
        this.setCoordinateIndex(i+21, n+7);
        this.setCoordinateIndex(i+22, n+7);
        this.setCoordinateIndex(i+23, n+4);
*/
        this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+2);
        this.setCoordinateIndex( i+3, n+3);
        
        this.setCoordinateIndex( i+4, n+4);
        this.setCoordinateIndex( i+5, n+5);
        this.setCoordinateIndex( i+6, n+1);
        this.setCoordinateIndex( i+7, n+0);
        
        this.setCoordinateIndex( i+8, n+5);
        this.setCoordinateIndex( i+9, n+6);
        this.setCoordinateIndex(i+10, n+2);
        this.setCoordinateIndex(i+11, n+1);
        
        this.setCoordinateIndex(i+12, n+6);
        this.setCoordinateIndex(i+13, n+7);
        this.setCoordinateIndex(i+14, n+3);
        this.setCoordinateIndex(i+15, n+2);
        
        this.setCoordinateIndex(i+16, n+7);
        this.setCoordinateIndex(i+17, n+4);
        this.setCoordinateIndex(i+18, n+0);
        this.setCoordinateIndex(i+19, n+3);
        
        this.setCoordinateIndex(i+20, n+7);
        this.setCoordinateIndex(i+21, n+6);
        this.setCoordinateIndex(i+22, n+5);
        this.setCoordinateIndex(i+23, n+4);
		
		for (int k=i;k<i+4;k++){
	        this.setColorIndex(0+k,10);
	        this.setColorIndex(4+k,11);
	        this.setColorIndex(8+k,12);
	        this.setColorIndex(12+k,11);
	        this.setColorIndex(16+k,12);
	        this.setColorIndex(20+k,13);
		}

		return i+24;
    }
    
    public void setCoorInCamara(int i){
		int n = n3;
/*
        this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+1);
        this.setCoordinateIndex( i+3, n+2);
        this.setCoordinateIndex( i+4, n+2);
        this.setCoordinateIndex( i+5, n+3);
        this.setCoordinateIndex( i+6, n+3);
        this.setCoordinateIndex( i+7, n+0);
        
        this.setCoordinateIndex( i+8, n+0);
        this.setCoordinateIndex( i+9, n+4);
        this.setCoordinateIndex(i+10, n+1);
        this.setCoordinateIndex(i+11, n+5);
        this.setCoordinateIndex(i+12, n+2);
        this.setCoordinateIndex(i+13, n+6);
        this.setCoordinateIndex(i+14, n+3);
        this.setCoordinateIndex(i+15, n+7);
        
        this.setCoordinateIndex(i+16, n+4);
        this.setCoordinateIndex(i+17, n+5);
        this.setCoordinateIndex(i+18, n+5);
        this.setCoordinateIndex(i+19, n+6);
        this.setCoordinateIndex(i+20, n+6);
        this.setCoordinateIndex(i+21, n+7);
        this.setCoordinateIndex(i+22, n+7);
        this.setCoordinateIndex(i+23, n+4);
*/
//        this.setCoordinateIndex(i+24, n3+8);
//        this.setCoordinateIndex(i+25, n3+9);

        this.setCoordinateIndex(   i, n+0);
        this.setCoordinateIndex( i+1, n+1);
        this.setCoordinateIndex( i+2, n+2);
        this.setCoordinateIndex( i+3, n+3);
        
        this.setCoordinateIndex( i+4, n+4);
        this.setCoordinateIndex( i+5, n+5);
        this.setCoordinateIndex( i+6, n+1);
        this.setCoordinateIndex( i+7, n+0);
        
        this.setCoordinateIndex( i+8, n+5);
        this.setCoordinateIndex( i+9, n+6);
        this.setCoordinateIndex(i+10, n+2);
        this.setCoordinateIndex(i+11, n+1);
        
        this.setCoordinateIndex(i+12, n+6);
        this.setCoordinateIndex(i+13, n+7);
        this.setCoordinateIndex(i+14, n+3);
        this.setCoordinateIndex(i+15, n+2);
        
        this.setCoordinateIndex(i+16, n+7);
        this.setCoordinateIndex(i+17, n+4);
        this.setCoordinateIndex(i+18, n+0);
        this.setCoordinateIndex(i+19, n+3);
        
        this.setCoordinateIndex(i+20, n+7);
        this.setCoordinateIndex(i+21, n+6);
        this.setCoordinateIndex(i+22, n+5);
        this.setCoordinateIndex(i+23, n+4);
		
		for (int k=i;k<i+4;k++){
	        this.setColorIndex(0+k,14);
	        this.setColorIndex(4+k,15);
	        this.setColorIndex(8+k,16);
	        this.setColorIndex(12+k,15);
	        this.setColorIndex(16+k,16);
	        this.setColorIndex(20+k,17);
		}

    }

    public void setRotSup(double a){
		if( a>180 ){ a=a-360; }
		if( a<-180 ){ a=a+360; }
    	rot_sup = a;
		calcularPmed();
		calcularPcam();
//		calcularVcam();
		setCoorParteSup();
		setCoorParteMed();
		setCoorParteInf();
		setCoorCamara();
    }

    public void setIncSup(double a){
		if( Math.abs(a)<=110 ){
	    	inc_sup = a;
			calcularPmed();
			calcularPcam();
//			calcularVcam();
			setCoorParteMed();
			setCoorParteInf();
			setCoorCamara();
		}
    }
    
    public void setIncMed(double a){
		if( Math.abs(a)<=110 ){
	    	inc_med = a;
			calcularPcam();
//			calcularVcam();
			setCoorParteInf();
			setCoorCamara();
		}
    }
    
    public void setRotCam(double a){
		if( a>180 ){ a=a-360; }
		if( a<-180 ){ a=a+360; }
    	rot_cam = a;
//		calcularVcam();
		setCoorCamara();
    }

    private double rot_sup=0;
    public double getRotSup() { return rot_sup; }

    private double inc_sup=45;
    public double getIncSup() { return inc_sup; }

    private double inc_med=80;
    public double getIncMed() { return inc_med; }

    private double rot_cam=0;
    public double getRotCam() { return rot_cam; }
    
//    public double getIncCam() { return rot_cam; }

    final static Point3d p_sup = new Point3d(0,9.4,0);
    private Point3d p_med = new Point3d();

    private Point3d p_cam = new Point3d();
    public Point3d getPcam() { return p_cam; }

    private Vector3d v_cam = new Vector3d();
    public Vector3d getVcam() {
    	calcularVcam();
    	return v_cam; 
    }
    
    private Vector3d u_cam = new Vector3d();

    final static double largo_bra_sup=4.498;
    final static double largo_bra_inf=1.65;
    final static double largo_camara=1.5;
    final static double ancho_bra_sup=.74;
    
    private int n1;
    private int n2;
    private int n3;
}